package org.jeecg.modules.haikrlk.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.haikrlk.entity.AgHaikrlk;
import org.jeecg.modules.haikrlk.service.IAgHaikrlkService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.hkvs.HaikService;
import org.jeecg.modules.lidyhxx.service.ILidyhxxService;
import org.jeecg.modules.shangt.entity.ShangtUser;
import org.jeecg.modules.shangt.service.ShangTApiHelper;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 海康人脸库
 * @Author: jeecg-boot
 * @Date: 2020-12-02
 * @Version: V1.0
 */
@Api(tags = "海康人脸库")
@RestController
@RequestMapping("/haikrlk/agHaikrlk")
@Slf4j
public class AgHaikrlkController extends JeecgController<AgHaikrlk, IAgHaikrlkService> {
    @Autowired
    private IAgHaikrlkService agHaikrlkService;

    @Autowired
    private ILidyhxxService lidyhxxService;

    @Autowired
    ShangTApiHelper shangTApiHelper;

    @Autowired
    HaikService haikService;

    @GetMapping(value = "/bukHaikrl")
    public Result<?> bukHaikrl() {
        agHaikrlkService.haikbk();
        return Result.ok();
    }

    @GetMapping(value = "/removeAllHaikrl")
    public Result<?> removeAllHaikrl() {
        return Result.ok(agHaikrlkService.batchDelete());
    }

    @GetMapping(value = "/syncLida")
    public Result<?> syncLida() {
        lidyhxxService.list().stream().forEach(e -> {
            AgHaikrlk agHaikrlk = new AgHaikrlk();

            AgHaikrlk test = agHaikrlkService.getOne(new QueryWrapper<AgHaikrlk>().eq("renybh",
                    e.getMemberCode()), false);

            if(test != null) {
//                BeanUtils.copyProperties(test, agHaikrlk);
//
//                agHaikrlk.setZhaop(e.getTupdz());
//                agHaikrlk.setRenybh(e.getMemberCode());
//                agHaikrlk.setXingm(e.getMemberName());
//                agHaikrlk.setShifbk("0");
//                agHaikrlk.setShenf("1");
//                agHaikrlk.setTongbsj(new Date());
//
//                agHaikrlkService.updateById(agHaikrlk);
//                log.info("同步理达数据，更新成功");

            } else {
                agHaikrlk.setZhaop(e.getTupdz());
                agHaikrlk.setRenybh(e.getMemberCode());
                agHaikrlk.setXingm(e.getMemberName());
                agHaikrlk.setShifbk("0");
                agHaikrlk.setShenf("1");
                agHaikrlk.setTongbsj(new Date());

                agHaikrlkService.save(agHaikrlk);
                log.info("同步理达数据，新增成功");
            }
        });

        return Result.ok();
    }

    // 同步商汤访客
    @GetMapping(value = "/syncShangt")
    public Result<?> syncShangt(String type) {
        agHaikrlkService.haiktb(type);
        return Result.ok();
    }

    /**
     * 分页列表查询
     *
     * @param agHaikrlk
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "海康人脸库-分页列表查询")
    @ApiOperation(value = "海康人脸库-分页列表查询", notes = "海康人脸库-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AgHaikrlk agHaikrlk,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AgHaikrlk> queryWrapper = QueryGenerator.initQueryWrapper(agHaikrlk, req.getParameterMap());
        Page<AgHaikrlk> page = new Page<AgHaikrlk>(pageNo, pageSize);
        IPage<AgHaikrlk> pageList = agHaikrlkService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param agHaikrlk
     * @return
     */
    @AutoLog(value = "海康人脸库-添加")
    @ApiOperation(value = "海康人脸库-添加", notes = "海康人脸库-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AgHaikrlk agHaikrlk) {
        agHaikrlkService.save(agHaikrlk);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param agHaikrlk
     * @return
     */
    @AutoLog(value = "海康人脸库-编辑")
    @ApiOperation(value = "海康人脸库-编辑", notes = "海康人脸库-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AgHaikrlk agHaikrlk) {
        agHaikrlkService.updateById(agHaikrlk);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "海康人脸库-通过id删除")
    @ApiOperation(value = "海康人脸库-通过id删除", notes = "海康人脸库-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        agHaikrlkService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "海康人脸库-批量删除")
    @ApiOperation(value = "海康人脸库-批量删除", notes = "海康人脸库-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.agHaikrlkService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "海康人脸库-通过id查询")
    @ApiOperation(value = "海康人脸库-通过id查询", notes = "海康人脸库-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AgHaikrlk agHaikrlk = agHaikrlkService.getById(id);
        if (agHaikrlk == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(agHaikrlk);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param agHaikrlk
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AgHaikrlk agHaikrlk) {
        return super.exportXls(request, agHaikrlk, AgHaikrlk.class, "海康人脸库");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AgHaikrlk.class);
    }

}
